<?php
/**
* @version		1.0  - 	Joomla 1.5.x
* @package	Component Com Products
* @copyright	Wampvn Group
* @license		GNU/GPL
* @website          http://wampvn.com
* @description    model category.
*/

defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.model' );
JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_products'.DS.'tables');	

class ModelProductCategory extends JModel
{
	var $_product = null;
	var $_info = null;
	var $_cid = null;
	var $_total = null;
	var $_imagesDefault =null;
	
	function __construct()
	{
		parent::__construct();

		$this->_cid = JRequest::getInt('catid', 0);
		$this->_size = JRequest::getVar('size', '');
		$this->_mid = JRequest::getVar('mid', '');
		
	}
	function getListProduct($limit, $limitstart = 0)	{
		
		if (!$this->_product)
		{
			$db =& JFactory::getDBO();				
			$query = "SELECT p.id, 
							 p.intro, 
							 p.saleprice, 
							 p.currency, 
							 p.catid, 
							 p.name, p.thumbnail, p.hits, p.monitorsize ,
                                                         c.name as catname
					FROM #__w_products as p, #__w_categories as c
					
					WHERE p.catid = c.id AND p.published = 1 ";
			if ($this->_cid > 0){
				$cats  = $this->getChildCat($this->_cid);
				$list = $this->_cid;
				if (!empty($cats)){
					foreach ($cats as $result){
						if (!empty($result->id)){
							$list .= ', ' .$result->id;
						}
					}
				}
				$query .= ' and p.catid IN ('.$list.')';
			}
			// search for size or manufacturer
			
			//$query .= " ORDER BY p.id ASC";
			$db->setQuery( $query, $limitstart, $limit);
			$this->_product = $db->loadObjectList();
		}
		
		
		
		
		return $this->_product;
	}	
	
	function getInfo()
	{
		if (!$this->_info)
		{
			$db =& JFactory::getDBO();				
			$query = "SELECT p.id, p.catid, p.monitorsize, m.name as manufacturer, m.id as manufacturerid 
			FROM #__w_products as p
			INNER JOIN #__w_manufacturers as m 
			ON p.manufacturerid = m.id 	
			
			
			WHERE p.published = 1 ";
			if ($this->_cid > 0){
				$query .= ' and p.catid = '.$this->_cid;
			}

			$db->setQuery( $query);
			$this->_info = $db->loadObjectList();
			
		
			
			
			
		}
		return $this->_info;	
	}
	
	function getImageDefault($proid){
		$this->_imagesDefault = '';
		if(!$this->_imagesDefault){
			$query = "SELECT filename FROM #__w_images WHERE proid=" .(int)$proid . " AND published =1 AND isdefault =1";
	
			$this->_db->setQuery($query);			
			$this->_imagesDefault = $this->_db->loadObject();
		}
		return $this->_imagesDefault;
	}
	
	
	
	
	function getCatName()
	{
			$db =& JFactory::getDBO();				
			$query = "SELECT name 
			FROM #__w_categories			
			WHERE published = 1 ";
			if ($this->_cid > 0){
				$query .= ' and id = '.$this->_cid;
			}
			$db->setQuery( $query);
			return $db->loadResult();		
	}
	
	
	
	function getManufacturerName($mid)
	{
			$db =& JFactory::getDBO();				
			$query = "SELECT name 
			FROM #__w_manufacturers			
			WHERE published = 1 ";
			if ($mid > 0){
				$query .= ' and id = '.$mid;
			}
			$query .= ' ORDER BY ordering ';
			$db->setQuery( $query);
			return $db->loadResult();		
	}

	/*function getManufacturers()
	{
			$db =& JFactory::getDBO();	
			$where = null;
			if ($this->_cid > 0){
				$where = ' and p.catid = '. $this->_cid;
			}
			$query = "SELECT m.id, m.name , count(p.manufacturerid) as amount 
			FROM #__w_products as p 
			INNER JOIN #__w_manufacturers as m
			ON p.manufacturerid = m.id			
			WHERE p.published = 1  $where 
			GROUP BY p.manufacturerid ";
			$db->setQuery($query);
			$this->_manufacturers = $db->loadObjectList();
			return $this->_manufacturers;
	}*/
	
	function getTotal()
	{
		$db =& JFactory::getDBO();
		$query = 'SELECT count(id) FROM #__w_products WHERE published = 1';
			if (!empty($this->_cid)){
				if ($this->_cid > 0){
					$cats  = $this->getChildCat($this->_cid);
					$list = $this->_cid;
					if (!empty($cats)){
						foreach ($cats as $result){
							if (!empty($result->id)){
								$list .= ', ' .$result->id;
							}
						}
					}
					$query .= ' and catid IN ('.$list.')';
				}
			}
			if (!empty($this->_size)){
				$query .= ' and LOWER(monitorsize) = '. $this->_size;				
			}
			if (!empty($this->_mid)){
				$query .= ' and manufacturerid = '. $this->_mid;				
			}	
		$db->setQuery( $query );
		return $db->loadResult();
	}
	
	function getCategory($cid)
	{
		if (!empty($catid))
		{
			$db =& JFactory::getDBO();
			$query = "SELECT * FROM #__w_categories WHERE id = $cid";
			$db->setQuery( $query );
			$result = $db->loadObject();
			return $result->name;		
		}
	}
	
	function getRating($productid){
		$db =& JFactory::getDBO();			
		$where = ' productid = '. $productid;		
		$query = "SELECT COUNT(rating) as count, SUM(rating) as total 
			FROM #__w_comments	
			WHERE published and $where	
			GROUP BY productid ";
		$db->setQuery($query);
		$row = $db->loadObject();

		if ($row->count){
			return round($row->total/$row->count,1);
		} else {
			return 0;
		}
	}
	
        //return array level 2 id
	function getChildCat($cid)
	{
			$db =& JFactory::getDBO();		
                        
                        //category level 1
			$query = "SELECT id " 
					. " FROM #__w_categories "			
					. " WHERE published = 1 and parentid = $cid ";
			$db->setQuery( $query);
			$arr_id_lvl1 = $db->loadObjectList();
                        
                        
                        //get category level 2
                        $tmp_arr_id = $arr_id_lvl1;
                        $arr_id_lvl2 = array();
                        
                        foreach($tmp_arr_id as $object){
                            $query = "SELECT id " 
                            		. " FROM #__w_categories "			
					. " WHERE published = 1 and parentid = $object->id ";
                            $db->setQuery($query);
                            $arr_id_lvl2[] = $db->loadObjectList();
                        }
                        
                        $tmp_arr_id = array();
                        foreach($arr_id_lvl2 as $arr){
                            $tmp_arr_id = array_merge($tmp_arr_id, $arr);
                        }
                        $arr_id_lvl2 = $tmp_arr_id;
                        
                        //merge two array => 
                        $arr_id = array_merge($arr_id_lvl1, array_merge($arr_id_lvl2));
                        
                        return $arr_id;
                        
	}
	function getImages($id){
		if(!$this->_images){
			$query = "SELECT 
							filename, isdefault 
					  FROM 
					  		#__w_images 
					  WHERE 
					  		proid=" .(int)$id . " AND published =1 ";
			$this->_db->setQuery($query);			
			$images = $this->_db->loadObjectList();
		}
		return $images;
	}
}

// end file
